package com.mamba.microservice.mybatis.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.mamba.microservice.mybatis.config.DruidDataSourceProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;

/**
 * Copyright ©2020 360jinrong All Rights Reserved
 * 请添加类/接口的说明：
 *
 * @Package: com.mamba.microservice.mybatis.utils
 * @author: fangjin
 * @date: 2020年05月12日 18时14分
 */
public class DbUtils {
    private final static ResourcePatternResolver RESOURCE_RESOLVER = new PathMatchingResourcePatternResolver();

    public static DataSource buildDruidDataSource(DruidDataSourceProperties druidDataSourceProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(druidDataSourceProperties.getUrl());
        dataSource.setDriverClassName(druidDataSourceProperties.getDriverClassName());
        dataSource.setUsername(druidDataSourceProperties.getUserName());
        dataSource.setPassword(druidDataSourceProperties.getPassword());

        Properties connectProperties = new Properties();
        connectProperties.setProperty("config.decrypt", druidDataSourceProperties.getDecrypt());
        connectProperties.setProperty("config.decrypt.key", druidDataSourceProperties.getPublicKey());
        dataSource.setConnectProperties(connectProperties);
        dataSource.setInitialSize(druidDataSourceProperties.getPoolInitSize());
        dataSource.setMinIdle(druidDataSourceProperties.getPoolMinIdle());
        dataSource.setMaxActive(druidDataSourceProperties.getPoolMaxActive());
        dataSource.setMaxWait(druidDataSourceProperties.getMaxWait());
        dataSource.setTimeBetweenConnectErrorMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        dataSource.setMaxEvictableIdleTimeMillis(3000000);
        dataSource.setTestWhileIdle(druidDataSourceProperties.getTestWhileIdle());
        dataSource.setTestOnBorrow(druidDataSourceProperties.getTestOnBorrow());
        dataSource.setTestOnReturn(druidDataSourceProperties.getTestOnReturn());
        dataSource.setTimeBetweenConnectErrorMillis(60000);
        return dataSource;
    }

    public static Resource[] resolveMapperLocations(String[] mapperLocations) {
        return Stream.of(Optional.ofNullable(mapperLocations).orElse(new String[0]))
                .flatMap(location -> Stream.of(getResources(location)))
                .toArray(Resource[]::new);
    }

    public static Resource resolveMybatisConfigResource(String mybatisConfigResource) {
        return RESOURCE_RESOLVER.getResource(mybatisConfigResource);
    }

    /**
     * 识别通配符的路径
     *
     * @param location
     * @return
     */
    private static Resource[] getResources(String location) {
        try {
            return RESOURCE_RESOLVER.getResources(location);
        } catch (IOException e) {
            return new Resource[0];
        }
    }

}
